package com.cenntro.nearr.googleMap;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.MessageFormat;
import java.text.ParseException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

//import com.qiuzhping.google.beans.GoogleGeocodeJSONBean;

/**
 * <Description functions in a word>
 * type :1-->address 2-->latlng
 * <Detail description>
 * 
 * @author  Peter.Qiu
 * @version  [Version NO, 2014-9-18]
 * @see  [Related classes/methods]
 * @since  [product/module version]
 */
public final class TestGoogleMap {

	private static Logger logger = LoggerFactory.getLogger(TestGoogleMap.class);
	private static final String GOOGLEAPIURL="http://ditu.google.cn/maps/api/geocode/json";  
	public static void getGoogleLatLng() {
	    CloseableHttpClient httpclient = HttpClients.createDefault();
        try {  
            // 创建httpget.    
            HttpGet httpget = new HttpGet(GOOGLEAPIURL + "?address=上海市&sensor=false&key=");  
            logger.debug("executing request " + httpget.getURI());  
            // 执行get请求.    
            CloseableHttpResponse response = httpclient.execute(httpget);  
           
            try {  
                // 获取响应实体    
                HttpEntity entity = response.getEntity();  
                logger.debug("--------------------------------------");  
                // 打印响应状态    
                System.out.println(response.getStatusLine());  
                if (entity != null) {  
                    // 打印响应内容    
                    String str = EntityUtils.toString(entity);
                    JSONObject o = (JSONObject) JSON.parse(str);
                    JSONArray o2 = (JSONArray) o.get("results");
                    JSONObject o3 =  (JSONObject) o2.get(0);
                    JSONObject o4 = (JSONObject) o3.get("geometry");
                    JSONObject o5 = (JSONObject)o4.get("location");
                    
                    logger.debug("lat====>>>"+o5.get("lat")+";lng=====>>>"+o5.get("lng"));
                }  
                logger.debug("------------------------------------");  
            } finally {  
                response.close();  
            }  
        } catch (ClientProtocolException e) {  
            e.printStackTrace();  
            logger.debug(e.getMessage());
        } catch (IOException e) {  
            e.printStackTrace();  
            logger.debug(e.getMessage());
        } finally {  
            // 关闭连接,释放资源    
            try {  
                httpclient.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
                logger.debug(e.getMessage());
            }  
        }
	}
	
	public static String getGoogleAddres(BigDecimal lat, BigDecimal lng) {
	    String addr = "";
	    if(null == lat || null == lng){
	        return addr;
	    }
	    CloseableHttpClient httpclient = HttpClients.createDefault();
        try {            
            
            // 创建httpget. 
            HttpGet httpget = new HttpGet(MessageFormat.format(GOOGLEAPIURL + "?latlng={0},{1}&sensor=false&&language=EN&key=", lat, lng));  
            logger.debug("executing request " + httpget.getURI());  
            // 执行get请求.
            CloseableHttpResponse response = httpclient.execute(httpget);  
           
            try {  
                // 获取响应实体    
                HttpEntity entity = response.getEntity();  
                logger.debug("--------------------------------------");  
                // 打印响应状态    
                System.out.println(response.getStatusLine());  
                if (entity != null) {  
                    // 打印响应内容    
                    String str = EntityUtils.toString(entity);
                    JSONObject o = (JSONObject) JSON.parse(str);
                    JSONArray o1 = (JSONArray)o.get("results");
                    
                    JSONObject o2 = (JSONObject)o1.get(0);
                    if(null != o2){
                        addr = String.valueOf(o2.get("formatted_address"));
                        logger.debug("详细地址====>>>"+addr);
                        JSONArray o3 = (JSONArray)o2.get("addressComponent");
                        logger.debug("地址明细====>>>"+JSONArray.toJSONString(o3));
                    }
                }
            } finally {
                response.close();  
            }  
        } catch (ClientProtocolException e) {  
            e.printStackTrace();  
            logger.debug(e.getMessage());
        } catch (IOException e) {  
            e.printStackTrace();  
            logger.debug(e.getMessage());
        } finally {  
            // 关闭连接,释放资源    
            try {  
                httpclient.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
                logger.debug(e.getMessage());
            }  
        }
        return addr;
    }
	
	public static void main(String[] args) {
		//29.583501, -95.418682
		BigDecimal lat  = new BigDecimal(29.583501);
		BigDecimal lng  = new BigDecimal(-95.418682);
		getGoogleAddres(lat, lng);
		System.out.println("########################");
		//getGoogleLatLng();
		
	}
}
